package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Upload {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/cet_word";
        String username = "root";
        String password = "123";
        String text = "";
        String correctAns = "";
        Integer id = 278;
        String optionA = "";
        String optionB = "";
        String optionC = "";
        String optionD = "";
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            // 解析文档内容并插入数据库
            try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\申鑫集团（未来五百强）\\Desktop\\阿波罗计划\\LeetCode\\springLearn\\src\\main\\java\\com\\test\\test.txt"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    line = line.trim();  // 去除行首尾的空格或换行符
                    // 使用正则表达式匹配顿号后的内容
                    Pattern pattern = Pattern.compile("^(.*?)、(.*)$");
                    Matcher matcher = pattern.matcher(line);
                    if (matcher.find()) {
                        String content = matcher.group(2); // 顿号后的内容
                        if (text.equals("")) {
                            text = content;
                        } else if (optionA.equals("")) {
                            optionA = content;
                        } else if (optionB.equals("")) {
                            optionB = content;
                        } else if (optionC.equals("")) {
                            optionC = content;
                        } else if (optionD.equals("")) {
                            optionD = content;
                        }
                    }
                    Pattern nextPattern = Pattern.compile("zzz(.*)$");
                    Matcher nextMatcher = nextPattern.matcher(line);
                    if (nextMatcher.find()) {
                        String ans = nextMatcher.group(1);
                        correctAns = ans;
                    }
                    if (!Objects.equals(text, "") &&
                            !Objects.equals(optionA, "") &&
                            !Objects.equals(optionB, "") &&
                            !Objects.equals(optionC, "") &&
                            !Objects.equals(optionD, "") &&
                            !Objects.equals(correctAns, "")) {
                        //TODO
                        System.out.println("text:" + text);
                        System.out.println(optionA);
                        System.out.println(optionB);
                        System.out.println(optionC);
                        System.out.println(optionD);
                        System.out.println(correctAns);
                        String sql = "INSERT INTO multiple_choice_questions (id,question_text,option_a,option_b,option_c,option_d,correct_option) VALUES (?,?,?,?,?, ?, ?)";
                        try (PreparedStatement statement = connection.prepareStatement(sql)) {
                            statement.setString(1, (id++).toString());
                            statement.setString(2, text);
                            statement.setString(3, optionA);
                            statement.setString(4, optionB);
                            statement.setString(5, optionC);
                            statement.setString(6, optionD);
                            statement.setString(7, correctAns);
                            statement.executeUpdate();
                        }
                        text = "";
                        optionA = "";
                        optionB = "";
                        optionC = "";
                        optionD = "";
                        correctAns = "";
                    }
                }
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}

class RegexExample {
    public static void main(String[] args) {
        String text = "1、垄断利润是垄断资本家凭借其在社会生产和流通中的垄断地位而获得的（）的高额利润。zzzC";

        // 正则表达式模式
        String patternString = "^(\\d+、)([A-Za-z]+、)(.+)(zzz)(.*)$";
        Pattern pattern = Pattern.compile(patternString);

        Matcher matcher = pattern.matcher(text);
        if (matcher.matches()) {
            String block1 = matcher.group(1); // 第一块内容
            String block2 = matcher.group(2); // 第二块内容
            String block3 = matcher.group(3); // 第三块内容
            String block4 = matcher.group(4); // 第四块内容
            String block5 = matcher.group(5); // 第五块内容
            System.out.println("Block 1: " + block1);
            System.out.println("Block 2: " + block2);
            System.out.println("Block 3: " + block3);
            System.out.println("Block 4: " + block4);
            System.out.println("Block 5: " + block5);
        }
    }
}
//                        text = matcher.group(2).trim();
//                        line = reader.readLine();
//                        if (line != null) {
//                            correctAns = line.trim().replace("zzz","");
//                            System.out.println("text: " + text);
//                            System.out.println("answer: " + correctAns);
//
////                             执行插入数据的SQL语句
////                            String sql = "INSERT INTO true_false_questions (id,question_text, correct_answer) VALUES (?, ?, ?)";
////                            try (PreparedStatement statement = connection.prepareStatement(sql)) {
////                                statement.setString(1, (id++).toString());
////                                statement.setString(2, text);
////                                statement.setString(3, correctAns.equals("正确") ? "1" : "0");
////                                statement.executeUpdate();
////                            }
//                        }